<template>
  <div class="level-list">
    <el-tabs v-model="activeName">
      <el-tab-pane label="基础信息" name="first">
        <levelForm :formInfo="formInfo" :levelType="levelType" :disabled="true" @saveData="saveData"></levelForm>
      </el-tab-pane>
      <el-tab-pane label="质控点信息" name="second">
        <!-- 搜索 -->
        <listSearch @search="search" @check="checkCallback()" :checkShow="isCheckShow"></listSearch>
        <!-- 表格 -->
        <myTable :column="column" :tableData="tableData" :total="total" @selectionChange="handleSelectionChange" @changePage="changePage" :selectionFilter="isCheckShow" :selection=true>
          <el-table-column label="操作" width="150">
            <template slot-scope="scope">
              <el-button type="text" @click="handle('detail', scope.row)">详情</el-button>
              <el-button v-if="scope.row.belongPart == userInfo.orgId" type="text" @click="handle('standard', scope.row)">设置标准值</el-button>
            </template>
          </el-table-column>
        </myTable>
      </el-tab-pane>
    </el-tabs>
    <div class="back-btn">
      <back-button></back-button>
    </div>

    <!-- 添加质控点 -->
    <addPoint v-if="addParams.visible" :addParams="addParams" :point-form="pointForm" @addOrEdit="addOrEdit" @getList="getList" @changePage="changePage"></addPoint>
    <!-- 设置标准值-->
    <setStandardValue v-if="dialogParams.visible" :dialog-params="dialogParams" :point-form="pointForm" @getList="getList"></setStandardValue>
  </div>
</template>

<script>
import listSearch from '@/components/search/listSearch'
import list from '@/components/cardToList/list'
import levelForm from '@/views/targetSystem/targetManage/module/levelFrom'
import addPoint from '@/views/targetSystem/targetManage/module/addPoint'
import setStandardValue from '@/views/targetSystem/standardManage/module/setStandardValue'
import myTable from '@/components/table/index.vue'
import { updateMyTargetData, getFormWorkById, getPointList, getMyPointDetail, delMyPointData, editMyPointData, updatePointInfoStandardValue } from '../../../api/target'
import { getOrganizeDetail } from '@/api/systemManage'
import { filterParams, getStorage } from '@/config/utils'

export default {
  name: 'standardManageList',
  components: {
    listSearch,
    list,
    addPoint,
    levelForm,
    setStandardValue,
    myTable
  },
  data() {
    return {
      levelType: '',
      activeName: 'first',
      column: [
        {
          prop: 'indexOne',
          label: '一级指标'
        },
        {
          prop: 'indexTwo',
          label: '二级指标'
        },
        {
          prop: 'indexThree',
          label: '三级指标'
        },
        {
          prop: 'name',
          label: '质控点'
        },
        {
          prop: 'intension',
          label: '质控点内涵'
        },
        {
          prop: 'diagnosisStandard',
          label: '诊断标准'
        },
        {
          prop: 'belongPartName',
          label: '责任部门'
        },
        {
          prop: 'valueType',
          label: '值类型'
        },
        {
          prop: 'expectedValue',
          label: '预期值'
        },
        {
          prop: 'standardValue',
          label: '标准值'
        },
        {
          prop: 'standardState',
          label: '确认状态',
          formatter: true
        },
        {
          prop: 'isWarning',
          label: '是否预警',
          asyncData: this.$config.yesOrNo
        },
        {
          prop: 'isOptional',
          label: '是否可选',
          asyncData: this.$config.isOptional
        }
      ],
      tableData: [],
      total: 0,
      multipleSelection: [],
      searchFrom: {
        targetStandardId: '',
        indexOne: '',
        indexTwo: '',
        indexThree: '',
        name: '',
        pageSize: 10,
        pageIndex: 1
      },
      addParams: {
        visible: false,
        title: '添加质控点',
        type: ''
      },
      formInfo: null,
      pointForm: {},
      dialogParams: {
        visible: false,
        editFlag: false,
        title: '设置标准值',
        type: 'standard',
        data: {}
      },
      isCheckShow: false,
      userInfo: getStorage('userInfo')
    }
  },
  created() {
    this.searchFrom.targetStandardId = this.$route.query.id
    this.levelType = this.$route.params.type
    this.showCheck()
    this.getList()
    this.getMyTargetById()
  },
  methods: {
    // 添加质控点
    handle(type, data) {
      switch (type) {
        case 'detail':
          this.addParams = {
            visible: true,
            title: '查看质控点',
            type: type,
            id: data,
            data: data
          }
          break
        case 'edit':
          this.addParams = {
            visible: true,
            title: '编辑质控点',
            type: type,
            id: data,
            data: data
          }
          break
        case 'del':
          this.$confirm('您确定要删除这些数据吗?', '批量删除', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
            .then(() => {
              delMyPointData(data)
                .then(res => {
                  this.$message.success('操作成功')
                  this.getList()
                })
                .catch()
            })
            .catch()
          break
        case 'standard':
          // sourceId不为空表示下发的质控点，为空或不存在就是自定义的质控点
          this.dialogParams = {
            visible: true,
            title: '设置标准值',
            pointType: type,
            type: type,
            data: data,
            id: data.id,
            editFlag: true,
            isCustomPoint: !(data.sourceId && data.sourceId > 0),
            rectificationRangeId: this.$route.query.rectificationRangeId,
            isWaitDeal: this.$route.query.isWaitDeal,
            waitDealId: this.$route.query.waitDealId
          }
          break
        case 'check':
          // 先检测每个质控点是否填写了标准值:
          for (const item of data) {
            if (!(item.standardValue || item.standardValue == 0)) {
              Message.error(`质控点"${item.name}"未设置标准值，不允许审核！`)
              return
            }
          }
          this.dialogParams = {
            visible: true,
            title: '管理员审核',
            okText: '通 过',
            type: type
          }
          break
      }
    },
    // 修改基础信息
    saveData(data) {
      updateMyTargetData(this.searchFrom.targetStandardId, data)
        .then(res => {
          this.$message.success('操作成功')
        })
        .catch()
    },
    // 查询质控点列表
    getList() {
      const params = filterParams(this.searchFrom)
      getPointList(params)
        .then(res => {
          this.tableData = res.dataList || []
          this.total = res.total || 0
        })
        .catch(() => {})
    },
    // 搜索
    search(searchFrom) {
      this.searchFrom.pageIndex = 1
      this.searchFrom.indexOne = searchFrom.indexOne
      this.searchFrom.indexTwo = searchFrom.indexTwo
      this.searchFrom.indexThree = searchFrom.indexThree
      this.searchFrom.name = searchFrom.name
      this.searchFrom.belongPart = searchFrom.belongPart
      this.getList()
    },
    // 查询我的目标标准详情
    getMyTargetById() {
      if (this.searchFrom.targetStandardId) {
        getFormWorkById(this.searchFrom.targetStandardId)
          .then(res => {
            this.formInfo = res || {}
          })
          .catch(() => {})
      }
    },
    // 获取质控点详情
    getDetail() {
      getMyPointDetail(this.addParams.id)
        .then(res => {
          if (res) {
            this.pointForm = { ...res }
          }
        })
        .catch(() => {})
    },
    // 编辑或新增质控点
    addOrEdit(type, params) {
      switch (type) {
        case 'edit':
          editMyPointData(this.addParams.id, params)
            .then(res => {
              this.$message.success('操作成功')
              this.addParams.visible = false
              this.getList()
            })
            .catch(() => {})
          break
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 切换分页
    changePage(searchForm) {
      this.searchFrom.pageIndex = searchForm.pageIndex
      this.searchFrom.pageSize = searchForm.pageSize
      this.getList()
    },
    showCheck() {
      getOrganizeDetail(this.userInfo.orgId).then(org => {
        if (org && org.chargePerson === this.userInfo.id) {
          this.isCheckShow = true
        }
      })
    },
    checkCallback() {
      const ids = []
      for (const row of this.multipleSelection) {
        if (!row.standardValue || row.standardValue == '') {
          this.$message.warning(`质控点“${row.name}”的标准值未设置！`)
          return
        }
        ids.push(row.id)
      }
      updatePointInfoStandardValue({
        standardState: 1,
        ids: ids
      })
        .then(res => {
          this.$message.success('质控点审核成功！')
          this.getList()
        })
        .catch(() => {})
    }
  },
  watch: {
    $route: {
      handler(val) {
        // 如果index为2,默认打开second的tab页，默认显示质控点信息
        if (val.query.index === 2) {
          this.activeName = 'second'
        }
      },
      deep: true,
      immediate: true
    }
  }
}
</script>

<style scoped>
</style>
